/**
 * 
 */
package fhq.hcmute.qrcode.reedsolomon;

import fhq.hcmute.qrcode.util.QRCodeException;
import static fhq.hcmute.qrcode.util.QRCodeException.*;
/**
 * @author Hien Nguyen Le
 * @Email: nglehien@gmail.com
 */
public class Monomial {
	private int exponent;
	private int coefficient;

	public Monomial() {
		this.exponent = formatExponent(0);
		this.coefficient = 0;
	}

	public Monomial(int coefficient, int exponent) {
		super();
		this.exponent = formatExponent(exponent);
		this.coefficient = coefficient;
	}

	private int formatExponent(int exponent) {
		if (exponent > 255) {
			return (int) (exponent % 256 + Math.floor(exponent / 256));
		}
		return exponent;
	}

	public Monomial multiply(Monomial monomial) throws QRCodeException {
		int coefficient = 0;
		int exponent = 0;
		coefficient = GaloisFiled256.multiply(this.coefficient,
				monomial.getCoefficient());
		exponent = this.exponent + monomial.exponent;
		return new Monomial(coefficient, exponent);
	}

	public Monomial xor(Monomial monomial) throws QRCodeException {
		int exponent = monomial.exponent;
		int coefficient = monomial.coefficient;
		if (this.exponent != exponent) {
			throw new QRCodeException(UNEQUAL_EXPONENT);
		}
		return new Monomial(coefficient ^ this.coefficient, exponent);
	}

	public int getExponent() {
		return exponent;
	}

	public void setExponent(int exponent) {
		this.exponent = exponent;
	}

	public int getCoefficient() {
		return coefficient;
	}

	public void setCoefficient(int coefficient) {
		this.coefficient = coefficient;
	}

	public int getCoeffExponent() {
		return GaloisFiled256.getLog(coefficient);
	}

	public void setCoeffExponent(int CoeffExponent) throws QRCodeException {
		this.coefficient = GaloisFiled256.getExp(CoeffExponent);
	}

	@Override
	public String toString() {
		return String.format("%dX^%d", coefficient, exponent);
	}
}
